package com.cykj.enterprise.service.impl;

import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.InterviewDetailDO;
import com.cykj.common.entity.UserDO;
import com.cykj.enterprise.mapper.SelfMapper;
import com.cykj.enterprise.service.ISelfService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: smart_recruit_backend
 * @BelongsPackage: com.cykj.enterprise.service.impl
 * @Author: 蔡张君逸
 * @CreateTime: 2025-08-21 08:41
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class SelfServiceImpl implements ISelfService {
    @Autowired
    private SelfMapper selfMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @Override
    public ResponseDTO getInterviews(int hrId) {
        List<InterviewDetailDO>  interviewDetailDOList=selfMapper.getInterviews(hrId);
        return ResponseDTO.success(new PageInfo<>(interviewDetailDOList));
    }

    @Override
    public ResponseDTO updateInterviews(int bookingId, int status) {
        System.err.println(selfMapper.updateInterview(bookingId,status));
        return ResponseDTO.success();
    }
//收藏相关
    @Override
    public ResponseDTO getCollectedUsers(int hrId) {
        List<UserDO> userDOS=selfMapper.getCollectedUsers(hrId);
        return ResponseDTO.success(new PageInfo<>(userDOS));
    }

    @Override
    public ResponseDTO delCollectedUser(int userId, int hrId) {
        if(redisTemplate.opsForValue().get("hrId:"+hrId+"userInfo:"+userId)!=null) {
            UserDO user= objectMapper.convertValue( redisTemplate.opsForValue().get("hrId:"+hrId+"userInfo:"+userId),new TypeReference<UserDO>(){});
            user.setIsCollected(0);
            redisTemplate.opsForValue().set("hrId:"+hrId+"userInfo:"+userId,user);
        }
        int res= selfMapper.delCollectedUser(userId,hrId);
        return ResponseDTO.success(res);
    }

    @Override
    public ResponseDTO collectUser(int userId, int hrId) {
        if(redisTemplate.opsForValue().get("hrId:"+hrId+"userInfo:"+userId)!=null) {
            UserDO user= objectMapper.convertValue( redisTemplate.opsForValue().get("hrId:"+hrId+"userInfo:"+userId),new TypeReference<UserDO>(){});
            user.setIsCollected(1);
            redisTemplate.opsForValue().set("hrId:"+hrId+"userInfo:"+userId,user);
        }
        int res=selfMapper.collectUser(userId,hrId);
        return ResponseDTO.success();
    }
//拉黑相关
    @Override
    public ResponseDTO getBlockUsers(int hrId) {
        List<UserDO> userDOS=selfMapper.getBlockUsers(hrId);
        return ResponseDTO.success(new PageInfo<>(userDOS));
    }

    @Override
    public ResponseDTO addInterviews(InterviewDetailDO interview) {
        int res=selfMapper.addInterviews(interview);
        return ResponseDTO.success(interview);
    }

    @Override
    public ResponseDTO delBlockUser(int userId, int hrId) {
        if(redisTemplate.opsForValue().get("hrId:"+hrId+"userInfo:"+userId)!=null) {
            UserDO user= objectMapper.convertValue( redisTemplate.opsForValue().get("hrId:"+hrId+"userInfo:"+userId),new TypeReference<UserDO>(){});
            user.setIsBlocked(0);
            redisTemplate.opsForValue().set("hrId:"+hrId+"userInfo:"+userId,user);
        }
        int res= selfMapper.delBlockUser(userId,hrId);
        return ResponseDTO.success();
    }

    @Override
    public ResponseDTO blockUser(int userId, int hrId) {
        if(redisTemplate.opsForValue().get("hrId:"+hrId+"userInfo:"+userId)!=null) {
            UserDO user= objectMapper.convertValue( redisTemplate.opsForValue().get("hrId:"+hrId+"userInfo:"+userId),new TypeReference<UserDO>(){});
            user.setIsBlocked(1);
            redisTemplate.opsForValue().set("hrId:"+hrId+"userInfo:"+userId,user);
        }
        int res=selfMapper.blockUser(userId,hrId);
        return ResponseDTO.success();
    }


}